package com.ama.controller;

import com.ama.pojo.Company;
import com.ama.pojo.Person;
import com.ama.service.CompanyService;
import com.ama.service.PersonService;
import com.ama.utils.SpringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class CancelController {
    @RequestMapping("/cancelInfo")
    public void cancelInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String type = request.getParameter("type");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<script>");

        if("个人".equals(type)){
            Person person = (Person) session.getAttribute("person");
            if(person.getPubtime()==null){
                out.println("alert('您还未布过简历！')");
            }else{
                SpringUtils.getBean(PersonService.class).deleteResume(person);
                person.setPubtime(null);
                session.setAttribute("person", person);
                out.println("alert('撤销成功！')");
            }
            out.println("window.open ('"+request.getContextPath()+"/index.jsp','_top')");
        }

        if("公司".equals(type)){
            Company company = (Company) session.getAttribute("company");
            if(company.getPubtime()==null){
                out.println("alert('您还未布过招聘信息！')");
            }else{
                SpringUtils.getBean(CompanyService.class).deleteInfo(company);
                company.setPubtime(null);
                session.setAttribute("company", company);
                out.println("alert('撤销成功！')");
            }
            out.println("window.open ('"+request.getContextPath()+"/c_index.jsp','_top')");
        }
        out.println("</script>");
        out.println("</html>");
        out.flush();
        out.close();
    }
}
